package com.wu;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.wu.db.DatabaseHelper;

public class CpActivity extends Activity {

	Button insert;
	Button query;
	DatabaseHelper databaseHelper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.cp);
		
		insert=(Button) findViewById(R.id.insert);
		query =(Button) findViewById(R.id.query);
		
		databaseHelper = new DatabaseHelper(CpActivity.this, "test_user");
		
		insert.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Log.d("me", "insert");
				ContentValues values=new ContentValues();
				values.put("username", "fay");
				values.put("email", "fay@111.com");
				databaseHelper.getWritableDatabase().insert("user", null, values);
			}
		});
		
		query.setOnClickListener(queryListener);
		
	}

	OnClickListener queryListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Log.d("me", "query");
			Cursor c = databaseHelper.getReadableDatabase().query("user", new String[]{"username","email"}, null, null, null, null, null);
			while (c.moveToNext()) {
				c.getString(c.getColumnIndex("username"));
				c.getString(c.getColumnIndex("email"));
				
				Toast.makeText(getApplicationContext(), c.getString(c.getColumnIndex("username")), Toast.LENGTH_SHORT).show();
				
			}
		}
	};
}
